OsmRx example¶

Imports and prepare input parameters¶

In [1]:
from IPython.display import display

from bokeh.plotting import output_notebook

output_notebook()
Loading BokehJS ...
In [2]:
# Set a location name
location = "Roanne"

Get POIs¶

In [3]:
%%time
from osmrx import Pois

pois_object = Pois()
pois_object.from_location(location)

# Get the roads data: a list of dict containing the geometry and the attributes
pois_data = pois_object.data
2023-04-07 21:09:06 - Pois            - INFO     : Building OsmFeatureModes.poi Data
2023-04-07 21:09:06 - Pois            - INFO     : Building the query
2023-04-07 21:09:09 - Pois            - INFO     : NominatimApi: Query 200:OK in 2.28 sec.
2023-04-07 21:09:09 - Pois            - INFO     : From Roanne
2023-04-07 21:09:09 - Pois            - INFO     : Building the query
2023-04-07 21:09:09 - Pois            - INFO     : Execute the query
2023-04-07 21:09:10 - Pois            - INFO     : OverpassApi: Query 200:OK in 0.65 sec.
CPU times: user 427 ms, sys: 103 ms, total: 530 ms
Wall time: 3.63 s

Get Roads¶

In [4]:
%%time
from osmrx import Roads

# Let's to get the roads network and connect POIs found on the same location
vehicle_object = Roads("vehicle",
                        pois_object.data)
vehicle_object.from_location(location)

# Get the roads data: a list of dict containing the geometry and the attributes
roads_data = vehicle_object.data
2023-04-07 21:09:10 - Roads           - INFO     : Building OsmFeatureModes.vehicle Data
2023-04-07 21:09:10 - Roads           - INFO     : Building the query
2023-04-07 21:09:10 - Roads           - INFO     : NominatimApi: Query 502:Bad Gateway in 0.31 sec.
2023-04-07 21:09:16 - Roads           - INFO     : NominatimApi: Query 200:OK in 2.54 sec.
2023-04-07 21:09:16 - Roads           - INFO     : From Roanne
2023-04-07 21:09:16 - Roads           - INFO     : Building the query
2023-04-07 21:09:16 - Roads           - INFO     : Execute the query
2023-04-07 21:09:17 - Roads           - INFO     : OverpassApi: Query 200:OK in 0.79 sec.
2023-04-07 21:09:17 - Roads           - INFO     : Network cleaning...
2023-04-07 21:09:17 - Roads           - INFO     : Starting: Adding new nodes on the network
2023-04-07 21:09:17 - Roads           - INFO     : Find nearest line for each node
2023-04-07 21:09:18 - Roads           - INFO     : Split line
2023-04-07 21:09:18 - Roads           - INFO     : Starting: Find intersections
2023-04-07 21:09:18 - Roads           - INFO     : Done: Find intersections
2023-04-07 21:09:18 - Roads           - INFO     : Build lines
2023-04-07 21:09:19 - Roads           - INFO     : Graph built
CPU times: user 1.94 s, sys: 177 ms, total: 2.11 s
Wall time: 9.19 s

Display roads and nodes¶

In [5]:
%%time

from bokeh.plotting import show
import geopandas as gpd
from gdf2bokeh import Gdf2Bokeh

map_session = Gdf2Bokeh(
    "My network map",
    width=800,
    height=600,
    background_map_name="CARTODBPOSITRON"
)

map_session.add_layer_from_dict_list("Roads", roads_data, from_epsg=4326,
                                     color="black")
map_session.add_layer_from_dict_list("POIs", pois_data, from_epsg=4326,
                                     color="blue", size=9)

map_session.add_layers_on_maps()

show(map_session.figure)
CPU times: user 4.54 s, sys: 216 ms, total: 4.76 s
Wall time: 4.76 s

Check topology details¶

In [6]:
%%time

roads_topo_data = vehicle_object.topology_checker()

map_session = Gdf2Bokeh(
    "My topology network map",
    width=800,
    height=600,
    background_map_name="CARTODBPOSITRON"
)

map_session.add_layer_from_dict_list("Roads unchanged", roads_topo_data.lines_unchanged, from_epsg=4326,
                                     color="black")
map_session.add_layer_from_dict_list("Roads added", roads_topo_data.lines_added, from_epsg=4326,
                                     color="green")
map_session.add_layer_from_dict_list("Roads split", roads_topo_data.lines_split, from_epsg=4326,
                                     color="orange")
map_session.add_layer_from_dict_list("Intersections added", roads_topo_data.intersections_added, from_epsg=4326,
                                     color="red")
map_session.add_layer_from_dict_list("POIs added", roads_topo_data.nodes_added, from_epsg=4326,
                                     color="blue", size=9)

map_session.add_layers_on_maps()

show(map_session.figure)
2023-04-07 21:09:24 - Roads           - INFO     : Topology analysis built.
CPU times: user 2.98 s, sys: 4.96 ms, total: 2.98 s
Wall time: 2.97 s

Compute a shortest path¶

In [7]:
from osmrx.main.roads import GraphAnalysis


from_poi = pois_data[50]["geometry"]
to_poi = pois_data[60]["geometry"]

analysis_object = GraphAnalysis("pedestrian",
                                [from_poi, to_poi])
paths_found = analysis_object.get_shortest_path()
paths = [feature.path for feature in paths_found]

map_session = Gdf2Bokeh(
    "My computed path",
    width=800,
    height=600,
    background_map_name="CARTODBPOSITRON"
)

map_session.add_layer_from_geom_list("Path found", paths, from_epsg=4326,
                                     color="blue")
map_session.add_layer_from_geom_list("From", [from_poi], from_epsg=4326,
                                     color="red", size=9)
map_session.add_layer_from_geom_list("To", [to_poi], from_epsg=4326,
                                     color="green", size=9)

map_session.add_layers_on_maps()

show(map_session.figure)
2023-04-07 21:09:27 - GraphAnalysis   - INFO     : Building OsmFeatureModes.pedestrian Data
2023-04-07 21:09:27 - GraphAnalysis   - INFO     : Building the query
2023-04-07 21:09:27 - GraphAnalysis   - INFO     : From 46.027930663456964, 4.064266763456965, 46.042573236543035, 4.084132736543035
2023-04-07 21:09:27 - GraphAnalysis   - INFO     : Building the query
2023-04-07 21:09:27 - GraphAnalysis   - INFO     : Execute the query
2023-04-07 21:09:28 - GraphAnalysis   - INFO     : OverpassApi: Query 200:OK in 0.41 sec.
2023-04-07 21:09:28 - GraphAnalysis   - INFO     : Network cleaning...
2023-04-07 21:09:28 - GraphAnalysis   - INFO     : Starting: Adding new nodes on the network
2023-04-07 21:09:28 - GraphAnalysis   - INFO     : Find nearest line for each node
2023-04-07 21:09:28 - GraphAnalysis   - INFO     : Split line
2023-04-07 21:09:28 - GraphAnalysis   - INFO     : Starting: Find intersections
2023-04-07 21:09:28 - GraphAnalysis   - INFO     : Done: Find intersections
2023-04-07 21:09:28 - GraphAnalysis   - INFO     : Build lines
2023-04-07 21:09:28 - GraphAnalysis   - INFO     : Graph built
2023-04-07 21:09:28 - GraphAnalysis   - INFO     : Shortest path(s) built from POINT (4.069461 46.037379) to POINT (4.0789385 46.0331249).

Compute a shortest path¶

In [8]:
from bokeh.palettes import all_palettes

from_poi = pois_data[50]["geometry"]
 
analysis_object = GraphAnalysis("vehicle", [from_poi])
isochrones_built = analysis_object.isochrones_from_distance([0, 250, 500, 1000, 1500], 0.6)

isochrones = isochrones_built.data
for pos, feature in enumerate(isochrones):
    feature["color"] = all_palettes['RdBu'][len(isochrones)][::-1][pos]

map_session = Gdf2Bokeh(
    "My isochrone",
    width=800,
    height=600,
    background_map_name="CARTODBPOSITRON"
)

map_session.add_layer_from_dict_list("Isochrones", isochrones_built.data, from_epsg=4326,
                                     color="color", line_color="white", fill_alpha=0.6)
map_session.add_layer_from_geom_list("From", [from_poi], from_epsg=4326,
                                     color="red", size=9)

map_session.add_layers_on_maps()

show(map_session.figure)
2023-04-07 21:09:28 - GraphAnalysis   - INFO     : Building OsmFeatureModes.vehicle Data
2023-04-07 21:09:28 - GraphAnalysis   - INFO     : Building the query
2023-04-07 21:09:28 - GraphAnalysis   - INFO     : From 46.008440640237865, 4.040522640237865, 46.06631735976214, 4.098399359762136
2023-04-07 21:09:28 - GraphAnalysis   - INFO     : Building the query
2023-04-07 21:09:28 - GraphAnalysis   - INFO     : Execute the query
2023-04-07 21:09:29 - GraphAnalysis   - INFO     : OverpassApi: Query 200:OK in 0.43 sec.
2023-04-07 21:09:29 - GraphAnalysis   - INFO     : Network cleaning...
2023-04-07 21:09:29 - GraphAnalysis   - INFO     : Starting: Adding new nodes on the network
2023-04-07 21:09:29 - GraphAnalysis   - INFO     : Find nearest line for each node
2023-04-07 21:09:30 - GraphAnalysis   - INFO     : Split line
2023-04-07 21:09:30 - GraphAnalysis   - INFO     : Starting: Find intersections
2023-04-07 21:09:30 - GraphAnalysis   - INFO     : Done: Find intersections
2023-04-07 21:09:30 - GraphAnalysis   - INFO     : Build lines
2023-04-07 21:09:31 - GraphAnalysis   - INFO     : Graph built
2023-04-07 21:09:31 - GraphAnalysis   - INFO     : Isochrones [(0, 250), (250, 500), (500, 1000), (1000, 1500)] built from POINT (4.069461 46.037379).
In [ ]: